﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System.ComponentModel;
using DevExpress.XtraGrid.Views.Grid;
using XCI.Core;
using XCI.Sys.Forms;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Controls;
using XCI.Windows.Extensions;
using XCI.Windows.Helper;

namespace XCI.Sys.Controls
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class RecordHistoryGridControl : GridControlx
    {
        private string _primaryKey;
        public override string Id => "d995239cdae646f1a08020724c044c60";
        private readonly PageParam paged = new PageParam { PageIndex = 1, PageSize = 100 };
        private GridHelper<SysHistory> helper;

        /// <summary>
        /// 记录主键
        /// </summary>
        [Category("XCI")]
        [Description("记录主键")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string PrimaryKey
        {
            get => _primaryKey;
            set
            {
                _primaryKey = value;
                if (string.IsNullOrEmpty(_primaryKey)) return;
                if (helper == null)
                {
                    helper = new GridHelper<SysHistory>((GridView)MainView);
                    MainView.DoubleClick += MainView_DoubleClick;
                }

                var list = HistoryService.Instance.SelectByPrimaryKey(paged, PrimaryKey) ?? new PageList<SysHistory>();
                helper.SetDataSource(list);
            }
        }

        private void MainView_DoubleClick(object sender, System.EventArgs e)
        {
            helper.DblClickRow(e, () =>
            {
                var detailsForm = new FrmHistoryDetails(helper);
                detailsForm.ShowDialogOk();
            });
        }
    }
}